package com.sannuo.system.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sannuo.common.domain.Menu;
import com.sannuo.common.domain.Role;
import com.sannuo.common.domain.RoleMenu;
import com.sannuo.common.domain.vo.MenuVo;
import com.sannuo.common.result.Result;
import com.sannuo.common.result.ResultCode;
import com.sannuo.system.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping(value = "/Role")
@RestController
@Api(tags = "Role" ,description = "赵心涛")
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;


    @RequestMapping(value = "list" , method = RequestMethod.GET)
    @ApiOperation(value = "查询所以菜单信息",notes = "查询所有菜单信息",httpMethod = "GET")
    public Result roleService (Integer page,Integer limit,String searchParams) throws Exception{
        Page page1 = PageHelper.startPage(page,limit);
        List<Role> users= roleService.selectRole();
        System.out.println("--------**"+users+"**---------");
        PageInfo pageInfo =new PageInfo(users);
        return Result.ok(ResultCode.SUCCESS,pageInfo.getTotal(),users);
    }

    @RequestMapping(value = "add" , method = RequestMethod.POST)
    @ApiOperation(value = "添加",notes = "添加Role",httpMethod = "POST")
    public Result roleService (Role role) throws Exception{

        int i = roleService.addRole(role);

        return Result.ok(ResultCode.SUCCESS,i);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询", notes = "根据id查询", httpMethod = "GET")
    public Result roleServiceByid (Integer i) throws Exception{

        List<Role> users= roleService.selectRoleById(i);
        System.out.println("--------**"+users+"**---------");

        return Result.ok(ResultCode.SUCCESS,users);
    }




    @RequestMapping(value = "RoleVo", method = RequestMethod.GET)
    @ApiOperation(value = "查询RoleVo", notes = "查询RoleVo", httpMethod = "GET")
    public Result RoleVoService (Integer id) throws Exception{
        List<MenuVo> menuVo=new ArrayList<>();//接收类

        List<Menu> menu=roleService.menu_Role();//查询所以主菜单

        List<RoleMenu> roleMenus=roleService.selectRoleMenu(id);//查询权限



        for (Menu menu2 : menu) {
            MenuVo menuVo1=new MenuVo();
            menuVo1.setId(menu2.getMenuId());
            menuVo1.setTitle(menu2.getMenuName());
            menuVo1.setPId(menu2.getPId());
            menuVo1.setUrl(menu2.getUrl());
            menuVo1.setSeq(menu2.getSeq());
            menuVo1.setStatus(menu2.getStatus());
            menuVo1.setCreateTime(menu2.getCreateTime());
            menuVo1.setUpdateTime(menu2.getUpdateTime());
            menuVo1.setRemark(menu2.getRemark());
            menuVo1.setType(menu2.getType());
            for (RoleMenu roleMenu : roleMenus) {
                System.out.println("***************"+ menu2.getMenuId()+"=="+roleMenu.getMenuId());
                try {
                if (menu2.getMenuId()==roleMenu.getMenuId()){
                    menuVo1.setChecked(true);
                    break;
                }else {
                    menuVo1.setChecked(false);
                }
                } catch (Exception e) {
                    menuVo1.setChecked(false);
                }
                System.out.println("***************"+ menuVo1.getChecked());
            }


            List<MenuVo> menuVo3=new ArrayList<>();
            List<Menu> menu1=roleService.selectMenus(menu2.getMenuId());//通过主菜单id查询子菜单

            for (Menu vo : menu1) {
                MenuVo menuVo2=new MenuVo();
                menuVo2.setId(vo.getMenuId());
                menuVo2.setTitle(vo.getMenuName());
                menuVo2.setPId(vo.getPId());
                menuVo2.setUrl(vo.getUrl());
                menuVo2.setSeq(vo.getSeq());
                menuVo2.setStatus(vo.getStatus());
                menuVo2.setCreateTime(vo.getCreateTime());
                menuVo2.setUpdateTime(vo.getUpdateTime());
                menuVo2.setRemark(vo.getRemark());
                menuVo2.setType(vo.getType());
                for (RoleMenu roleMenu : roleMenus) {
                    try {
                    if (roleMenu.getMenuId()==vo.getMenuId()){
                        menuVo2.setChecked(true);
                        break;
                    }else {
                        menuVo2.setChecked(false);
                    }
                } catch (Exception e) {
                    menuVo2.setChecked(false);
                }
                }

                menuVo3.add(menuVo2);

            }

            menuVo1.setChildren(menuVo3);

            menuVo.add(menuVo1);
        }



        return Result.ok(ResultCode.SUCCESS,menuVo);
    }


    @RequestMapping(value = "shouquan", method = RequestMethod.GET)
    @ApiOperation(value = "根据id授权", notes = "根据id授权", httpMethod = "GET")
    public Result roleMenuUpdate (Integer roleId,int[] menuId) throws Exception{

        int i=roleService.deleteRoleMenu(roleId);

        for (int i1 : menuId) {
            int j=roleService.addRoleMenu(roleId,i1);
        }
        return Result.ok(ResultCode.SUCCESS,i);
    }

    @RequestMapping(value = "update" , method = RequestMethod.GET)
    @ApiOperation(value = "修改",notes = "通过id修改Role",httpMethod = "GET")
    public Result roleUpdate (Role role) throws Exception{
        System.out.println("----------***"+role);
        int i = roleService.RoleUpdate(role);

        return Result.ok(ResultCode.SUCCESS,i);
    }

}
